﻿Public Class ItemMasterExplorer
    Inherits ExplorerTreeList
    Implements IExplorer


    Private EditPanePL As PLBase
    Private WithEvents ClientSpec_LookupEdit As GridLookUpEdit

    Public ClientSpec_LookupView As GridView = Nothing

    Public Sub New(ByVal aQueryName As String)
        MyBase.New(aQueryName)

    End Sub

    Public Sub New()
        MyBase.New()
    End Sub



    ''' <summary>
    ''' This is where the the MetaAction are initialise and assigned to the two Actionlist by design
    ''' a) DefaultActionList : hardcoded bydesign
    ''' b) MetaControlActionList : defined by the MetaControl definition .
    ''' </summary>
    ''' <remarks>add your default Entityexplorer here if needed
    ''' EX:
    '''         Dim action As CommonExt.Base.ActionOption
    '''         action = New ActionOption("IM", "ACT_000xx", "Add New ProdOrder", AddressOf MenuNewProdOrder, True, True, "0")
    '''          MetaControlActionOptionList.Add(action) 
    ''' 
    ''' Refrer to : SSP_001234
    ''' </remarks>
    ''' 
    Public Overrides Sub ActionOptionLoad()
        MyBase.ActionOptionLoad()

        DefaultTreeListActionOptionList.ApplySecurity()

        'with the IExplorer interface on oublige que EntityActionOptionListLoad() existe
        MetaControlActionOptionListLoad()
        MetaControlTreeListActionOptionList.ApplySecurity()

    End Sub

    Public Overrides Sub PopulateDetailSection() Implements IExplorer.PopulateDetailSection
        Me.Cursor = Cursors.WaitCursor
        '
        If aGridControl IsNot Nothing Then
            aGridControl.Dispose()
            aGridControl = Nothing
        End If
        '
        Dim guidEnfant As String
        Dim aPLName As String

        Dim aPaneMetaQueryName As String
        Dim Ametaquerie As MetaQuery
        Dim frmCaption As String = "Item Master List"

        Try

            guidEnfant = DirectCast(NodeDataRow(Me.KeyFieldName), String)
            aPLName = DirectCast(NodeDataRow("PL"), String)
            imExplorer = New IMExplorerNodeInfo(NodeDataRow)



            aPaneMetaQueryName = DirectCast(NodeDataRow("MetaQuerie"), String)
            Ametaquerie = MetaQuery.GetMetaQueryByName(aPaneMetaQueryName)

            'c<est ici qu<on bind les info du noeud qui articule le Edit PANE
            EditPanePL = PLBase.CreateInstanceOf(aPLName, aPaneMetaQueryName)
            aGridControl = New GridExtPL() With {.Dock = DockStyle.Fill, .Pl = EditPanePL, .SecureGrid = True, .Name = Ametaquerie.Caption, .FormCtrlCD = MyBase.PanelName}

            If NodeDataRow.Table.Columns.Contains(".") AndAlso NodeDataRow.Table.Columns.Contains("Description_PRM") Then
                frmCaption = BreadCrumbText
            Else
                frmCaption = BreadCrumbText
            End If

            'scoping will be done on each node 
            'Last node is a display and KEYID is FORCED as newguid for display not scoping right grid detail
            Select Case aPLName
                Case "Site_PL"
                    Dim aID As Guid
                    If System.Guid.TryParse(guidEnfant, aID) Then
                        If Not Guid.Empty.Equals(aID) Then
                            EditPanePL.SQLWhere = String.Format("{0} = '{1}'", Ametaquerie.GUIDField, guidEnfant)
                        End If
                    End If
                Case "ItemMaster_PL"
                    'MQ: "ItemMaster"
                    Dim aID As Guid
                    If System.Guid.TryParse(guidEnfant, aID) Then

                        If Not Guid.Empty.Equals(aID) Then
                            EditPanePL.SQLWhere = String.Format("{0} = '{1}'", Ametaquerie.GUIDField, guidEnfant)
                        End If
                    End If

                    AddHandler aGridControl.InitNewMasterRow, AddressOf ItemMaster_InitNewMasterRow

                Case "ItemMasterRevision_PL"
                    'MQ:"ItemMasterRevision"
                    EditPanePL.SQLWhere = String.Format("{0} = '{1}'", Ametaquerie.GUIDField, imExplorer.NodeID)
                    AddHandler aGridControl.DetailCustomRowCellEditForEditing, AddressOf ItemMasterRevision_DetailCustomRowCellEditForEditing
                    AddHandler aGridControl.ProcessCurrentDetailRecord, AddressOf ItemMasterRevision_ProcessCurrentDetailRecord
                    AddHandler aGridControl.ShownDetailEditor, AddressOf ItemMasterRevision_ShownDetailEditor

                    aGridControl.GridActionOptionList.Add(New ActionOption("COA", "ACT_00162", "Open with Designer", AddressOf ItemMasterDocument_ShowInDesigner, True, True, "ItemMasterDocument"))
                    aGridControl.GridActionOptionList.Add(New ActionOption("COA", "ACT_00110", "Refresh COA Query", AddressOf MenuRefreshCOAQuery, True, True, "ItemMasterDocument"))

                Case "BOMRevision_PL"
                    'MQ:"BOMRevision"
                    EditPanePL.SQLWhere = String.Format("{0} = '{1}' AND ItemMasterRevID = '{2}' ", Ametaquerie.GUIDField, imExplorer.NodeID, imExplorer.ParentID)

                Case "RoutingRevision_PL"
                    'MQ:"RoutingRevision"
                    EditPanePL.SQLWhere = String.Format("{0} = '{1}' AND ItemMasterRevID = '{2}' ", Ametaquerie.GUIDField, imExplorer.NodeID, imExplorer.ParentID)

                Case "ItemMasterSpecificationRevision_PL"
                    'MQ: "IMSpecRevision"
                    EditPanePL.SQLWhere = String.Format("{0} = '{1}' AND ItemMasterRevID = '{2}' ", Ametaquerie.GUIDField, imExplorer.NodeID, imExplorer.ParentID)

                Case "BOMRevisionDetail_PL"
                    'MQ: "IMBRevisionDetail"
                    EditPanePL.SQLWhere = String.Format("{0} = '{1}'", "BOMRevID_FK", imExplorer.NodeID)

                    AddHandler aGridControl.InitNewMasterRow, AddressOf BOMRevDetail_InitNewMasterRow
                    AddHandler aGridControl.MasterRowClick, AddressOf BOMRevDetail_MasterRowClick
                    AddHandler aGridControl.MainContextMenuBeforePopup, AddressOf BOMRevDetail_ContextMenuBeforePopup
                    aGridControl.GridActionOptionList.Add(New ActionOption("SpecTest", "ACT_00086", "Revise", AddressOf BOMRevDetail_Revise, True, True))
                    aGridControl.GridActionOptionList.Add(New ActionOption("SpecTest", "ACT_00087", "Limit", AddressOf BOMRevDetail_Limit, True, True))

                Case "RoutingRevisionDetail_PL"
                    'MQ:"IMRRevisionDetail"

                    EditPanePL.SQLWhere = String.Format("{0} = '{1}'", "RoutingRevID_FK", imExplorer.NodeID)

                    AddHandler aGridControl.InitNewMasterRow, AddressOf RoutingRevDetail_InitNewMasterRow
                    AddHandler aGridControl.MasterRowClick, AddressOf RoutingRevDetail_MasterRowClick
                    AddHandler aGridControl.MainContextMenuBeforePopup, AddressOf RoutingRevDetail_ContextMenuBeforePopup
                    AddHandler aGridControl.DetailCustomRowCellEditForEditing, AddressOf RoutingRevDetail_CustomRowCellEditForEditing

                    aGridControl.GridActionOptionList.Add(New ActionOption("SpecTest", "ACT_00088", "Revise", AddressOf RoutingRevDetail_Revise, True, True))
                    aGridControl.GridActionOptionList.Add(New ActionOption("SpecTest", "ACT_00089", "Limit", AddressOf RoutingRevDetail_Limit, True, True))
                    aGridControl.GridActionOptionList.Add(New ActionOption("RoutingDiag", "ACT_00185", "Routing Diagramme", AddressOf RoutingSheetDiagram, True, True, "RoutingSheet"))

                Case "ItemMasterSpecificationRevisionDetail_PL"
                    'MQ: "IMSRevisionDetail"

                    EditPanePL.SQLWhere = String.Format("IMSRD.ItemMasterSpecRevID_FK = '{0}' and IMR.ItemMasterRevID = '{1}'", imExplorer.NodeID, imExplorer.ItemMasterRevID)

                    AddHandler aGridControl.InitNewMasterRow, AddressOf SpecRevDetail_InitNewMasterRow
                    AddHandler aGridControl.MasterCellValueChanged, AddressOf SpecRevDetail_MasterCellValueChanged
                    AddHandler aGridControl.DetailCellValueChanged, AddressOf SpecRevDetail_DetailCellValueChanged
                    AddHandler aGridControl.MainContextMenuBeforePopup, AddressOf SpecRevDetail_ContextMenuBeforePopup
                    AddHandler aGridControl.ShownMasterEditor, AddressOf SpecRevDetail_ShownMasterEditor
                    AddHandler aGridControl.MasterRowClick, AddressOf SpecRevDetail_MasterRowClick
                    AddHandler aGridControl.MasterCustomRowCellEditForEditing, AddressOf SpecRevDetail_MasterCustomRowCellEditForEditing

                    'AddHandler aGridControl.MasterRowCellStyle, AddressOf SpecRevDetail_MasterRowCellStyle


                    aGridControl.GridActionOptionList.Add(New ActionOption("SpecTest", "ACT_00090", "Revise", AddressOf SpecRevDetail_Revise, True, True))
                    aGridControl.GridActionOptionList.Add(New ActionOption("SpecTest", "ACT_00091", "Limit", AddressOf SpecRevDetail_Limit, True, True))
            End Select

        Catch ex As Exception
            MessageWindow.ShowMessage(ex)

        End Try




        '
        'On trouve la form pour effacer les controle dedans et ensuite remettre le bon.
        'Comme ca on change le Detail selon la ligne dans L'explorer
        For Each aFrm As XtraForm In Me.ParentForm.MdiChildren
            If aFrm.Name = PanelName Then
                aFrm.Controls.Clear()
                aFrm.Controls.Add(aGridControl)
                aFrm.Text = frmCaption
                aFrm.Show()
                aFrm.Focus()
            End If
            aFrm.Refresh()
        Next


        If imExplorer.NodeActivated Then
            aGridControl.AllowCreate = False
            aGridControl.AllowDelete = False
            aGridControl.AllowUpdate = False
            Dim acolumns As GridColumnCollection = aGridControl.GridView1.Columns
            For Each col As GridColumn In acolumns
                col.OptionsColumn.ReadOnly = True
            Next
        End If

        imExplorer.Dispose()
        Me.Cursor = Cursors.Default
    End Sub


End Class
